package com.shen.educenter.controller;

import com.shen.commonutils.Result;
import com.shen.educenter.utils.ConstantWxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@CrossOrigin
@RestController//注意这里没有配置 @RestController
@RequestMapping("/educenter/api/ucenter/wx")
public class WxApiController2 {
    //1,生成二维码
    @GetMapping("getVXCodeUrl")
    public Result getVxCodeUrl() throws UnsupportedEncodingException {
        //固定地址 ，拼接参数---
//        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
//                "?appid="+ ConstantWxUtils.WX_OPEN_APP_ID +
//
//                "&redirect_uri="+ConstantWxUtils.WX_OPEN_REDIRECT_URL +
//
//                "&response_type=code";
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=%s" +

                "&redirect_uri=%s" +

                "&response_type=code" +

                "&scope=snsapi_login" +

                "&state=%s" +

                "#wechat_redirect";
        String redirectUrl = ConstantWxUtils.WX_OPEN_REDIRECT_URL; //获取业务服务器重定向地址
        redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8"); //url编码
        // 防止csrf攻击（跨站请求伪造攻击）
        //String state = UUID.randomUUID().toString().replaceAll("-", "");//一般情况下会使用一个随机数
//        String state = "imhelen";//为了让大家能够使用我搭建的外网的微信回调跳转服务器，这里填写你在ngrok的前置域名
//        System.out.println("state = " + state);
        String url = String.format(
                baseUrl,
                ConstantWxUtils.WX_OPEN_APP_ID,
                redirectUrl,
                "atguigu");
        return Result.ok().data("url",url);
    }

}
